package com.media.module.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

public class AudioUtils {

	public static void merge(String[] files, String outPath) throws FileNotFoundException, IOException {

		FileOutputStream fos = new FileOutputStream(outPath);
		for (int i = 0, len = files.length; i < len; i++) {
			File file = new File(files[i]);
			RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
			// Skip file header bytes,
			// amr file header's length is 6 bytes
			if (i > 0) {
				randomAccessFile.seek(5);
			}
			byte[] buffer = new byte[512];
			int count = 0;
			while ((count = randomAccessFile.read(buffer)) != -1) {
				fos.write(buffer, 0, count);
			}

			randomAccessFile.close();
			fos.flush();
			randomAccessFile.close();
		}

		fos.flush();
		fos.close();

	}
	
	public static void mergeFiles3(String[] tempFiles, String realFile) throws IOException {
			FileOutputStream fos = new FileOutputStream(realFile);
			RandomAccessFile ra = null;
			for (int i = 0; i < tempFiles.length; i++) {
				ra = new RandomAccessFile(tempFiles[i], "r");
				if (i != 0) {
					ra.seek(6);// 跳过amr文件的文件头

				}
				byte[] buffer = new byte[1024 * 8];
				int len = 0;
				while ((len = ra.read(buffer)) != -1) {
					fos.write(buffer, 0, len);
				}
			}
			ra.close();
			fos.close();
	}


	/**
	 * 合并AMR文件 ，
	 * 
	 * @param files
	 *            需要合成的两个文件
	 * @param outPath
	 *            输出路径
	 * @param ifDeleteOld
	 *            是否删除合成之前的文件，
	 * @throws IOException 
	 */
	public static void megerCatch(String[] files, String outPath) throws IOException {
		File outPutFile = new File(outPath);

		if (!outPutFile.exists()) {
			outPutFile.createNewFile();
		}
		FileOutputStream fileOutPutStream = new FileOutputStream(outPutFile);
		for (int i = 0; i < files.length; i++) {
			File file = new File(files[i]);
			FileInputStream fileInputStream = new FileInputStream(file);
			byte[] buff = new byte[1024];
			if (i == 0) {
				while (fileInputStream.read(buff) != -1) {
					fileOutPutStream.write(buff, 0, buff.length);

				}
			} else {
				while (fileInputStream.read(buff) != -1) {
					fileOutPutStream.write(buff, 6, buff.length - 6);

				}
			}
			fileOutPutStream.flush();
			fileInputStream.close();
		} 
		fileOutPutStream.close();

	}

	/**
	 * 通过FileChannel方式
	 */
	public static void mergeFiles2(String[] tempFiles, String realFile) {
		FileChannel mFileChannel;
		try {
			FileOutputStream fos = new FileOutputStream(realFile);
			mFileChannel = fos.getChannel();
			FileChannel inFileChannel;
			for (String file : tempFiles) {
				inFileChannel = new FileInputStream(file).getChannel();
				// 下面应该根据不同文件减去相应的文件头（这里没有剪去文件头，实际应用中应当减去）
				inFileChannel.transferTo(0, inFileChannel.size(), mFileChannel);
				inFileChannel.close();
			}
			fos.close();
			mFileChannel.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
